import 'package:container_tab_indicator/container_tab_indicator.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xvideos/assets_tool/images.dart';
import 'package:xvideos/hj_page/user_center_page/user_center_logic.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';
import 'package:xvideos/tools_base/global_store/store.dart';
import 'package:xvideos/tools_base/indicator/custom_tab_indicator.dart';
import 'package:xvideos/tools_base/widget/follow_button.dart';

import '../../../config/config.dart';
import '../../../tools_base/widget/net_image_widget.dart';

class UserCenterHeader extends StatefulWidget {
  final UserCenterController controller;

  UserCenterHeader({super.key, required this.controller});

  @override
  State<UserCenterHeader> createState() => _UserCenterHeaderState();
}

class _UserCenterHeaderState extends State<UserCenterHeader> {
  UserCenterController get logic => widget.controller;

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Color(0xff050f17),
      child: Stack(
        children: [
          Positioned(
            left: 0,
            right: 0,
            child: SizedBox(
              height: 190,
              child: Image.asset(" user_center_bg.webp".mineImgPath, fit: BoxFit.fill),
            ),
          ),
          Positioned(
            child: Container(
              width: double.infinity,
              height: 190,
              color: Color(0xff151515).withOpacity(0.1),
            ),
          ),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 12),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                (190 - 24).sizeBoxH,
                Container(
                  height: 80,
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: [
                      Container(
                        height: 80,
                        width: 80,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          border: Border.all(color: Color(0xff030F18)),
                          borderRadius: BorderRadius.circular(40),
                        ),
                        child: NetworkImageLoader(
                          imageUrl: logic.model?.portrait ?? "",
                          height: 74,
                          width: 74,
                          borderRadius: 40,
                        ),
                      ),
                      Spacer(),
                      if (!globalStore.isMe(logic.model?.uid)) ...[
                        InkWell(
                          onTap: () => logic.gotoPrivateChat(),
                          child: Image.asset("msg_button.webp".commonImgPath, width: 24),
                        ),
                        12.sizeBoxW,
                        FollowButton(
                          mediaId: (logic.model?.uid ?? 0).toString(),
                          followType: FollowEnum.user,
                          isFollow: logic.model?.isFollow,
                          successsAction: (isSuccess) => logic.model?.isFollow = isSuccess,
                        ),
                      ],
                    ],
                  ),
                ),
                12.sizeBoxH,
                Text(
                  "${logic.model?.name ?? ""}",
                  style: TextStyle(
                    color: Color(0xffEFEFEF),
                    fontSize: 16,
                    fontWeight: FontWeight.w500,
                  ),
                ),
                6.sizeBoxH,
                Text(
                  "${logic.model?.totalWorkCount ?? "0"} 作品    ${logic.model?.fans?.humanizeFormat ?? "0"}粉丝",
                  style: TextStyle(
                    color: Color(0xff656565),
                    fontSize: 12,
                  ),
                ),
                24.sizeBoxH,
                if (logic.model?.summary?.isNotEmpty == true)
                  Container(
                    padding: EdgeInsets.only(bottom: 24),
                    child: Text(
                      logic.model?.summary ?? "",
                      maxLines: 3,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(color: Color(0x99FFFFFF), fontSize: 12, height: 1.5),
                    ),
                  ),
              ],
            ),
          )
        ],
      ),
    );
  }
}

class UserCenterTabbar extends StatefulWidget {
  final Function(int index)? changeTab;
  final TabController controller;

  UserCenterTabbar(this.controller, {super.key, this.changeTab});

  @override
  State<UserCenterTabbar> createState() => _UserCenterTabbarState();
}

class _UserCenterTabbarState extends State<UserCenterTabbar> with TickerProviderStateMixin {
  late TabController mainTabCtr;
  int tabSelectedIndex = 0;
  int selectIndex = 0;

  @override
  void initState() {
    super.initState();
    mainTabCtr = widget.controller;
    mainTabCtr.addListener(() {
      selectIndex = 0;
      tabSelectedIndex = mainTabCtr.index;
      widget.changeTab?.call(selectIndex);
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return TabBar(
      isScrollable: true,
      tabAlignment: TabAlignment.start,
      controller: widget.controller,
      unselectedLabelStyle: TextStyle(color: Color(0xffcccccc), fontSize: 14, fontWeight: FontWeight.w400),
      labelPadding: EdgeInsets.only(bottom: 2),
      indicator: CustomIndicator(
        height: 4,
        width: 16,
        useGradient: true,
        gradientColors: [
          Color(0x00E1351F),
          Color(0xFFE1351F),
        ],
        offsetY: 4,
      ),
      labelStyle: TextStyle(color: Colors.white, fontSize: 14, fontWeight: FontWeight.w500),
      tabs: ['影视', '抖音', '帖子', '图集']
          .map((e) => Padding(
                padding: const EdgeInsets.symmetric(horizontal: 8.0),
                child: Text(e),
              ))
          .toList(),
    );
  }
}
